package Thread;
//互斥锁
public class Synchronized {
    public static void main(String[] args) {
        Foo foo = new Foo();
        Thread t1 = new Thread("线程1") {
            public void run() {
                foo.m1();
            }
        };
        Thread t2 = new Thread("线程2") {
            public void run() {
                foo.m2();

            }
        };
        t1.start();
        t2.start();
    }
}

    class Foo {
        public static synchronized void m1() {
            try {
                System.out.println("准备开始");
                Thread.sleep(5000);
                System.out.println("结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void m2() {
            synchronized (Foo.class) {
                try {
                    System.out.println("准备开始");
                    Thread.sleep(5000);
                    System.out.println("结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
